package com.chiwen.common.utils.sms;

import com.chiwen.common.utils.http.HttpUtilsCustom;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SmsUtil {
    private static final Logger log = LoggerFactory.getLogger(SmsUtil.class);

    @Value("${chiwen.sms.channel}")
    private String channel;

    @Value("${chiwen.sms.compony_sign}")
    private String componySign;

    private final static String SMS_URL = "https://yxsms.market.alicloudapi.com";

    /**
     * 发送短信-入口
     */
    public boolean sendSms(String mobile, String code) {
        if (componySign.equals("shanghai001")) {
            return sendSmsOne(mobile, code);
        } else if (componySign.equals("shangdong001")) {
            return sendSmsTwo(mobile, code);
        } else {
            return false;
        }
    }

    /**
     * 发送短信-上海供应商
     */
    public boolean sendSmsOne(String mobile, String code) {
        log.info("入参:[{},{}]", mobile, code);
        String path = "/sms";
        String method = "GET";
        String appcode = "765cb9f411f946e0badb3842cdad392d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("content", componySign + "验证码为：" + code + "，有效期1分钟，打死都不能告诉别人哦，感谢注册！");
        querys.put("phone", mobile);
        try {
            HttpResponse response = HttpUtilsCustom.doGet(SMS_URL, path, method, headers, querys);
            String resp = EntityUtils.toString(response.getEntity());
            log.info("接口返回:[{}]", resp);
        } catch (Exception e) {
            log.error("异常:{}", e);
            return false;
        }
        return true;
    }

    /**
     * 发送短信-山东供应商
     */
    public boolean sendSmsTwo(String mobile, String code) {
        log.info("入参:[{},{}]", mobile, code);
        String host = "http://dingxintz.market.alicloudapi.com";
        String path = "/dx/notifySms";
        String method = "POST";
        String appcode = "765cb9f411f946e0badb3842cdad392d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", "18666981551");
        querys.put("param", "name:张三");
        querys.put("tpl_id", "TP18040315");

        Map<String, String> bodys = new HashMap<String, String>();
        try {
            HttpResponse response = HttpUtilsCustom.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            String resp = EntityUtils.toString(response.getEntity());
            log.info("接口返回:[{}]", resp);
        } catch (Exception e) {
            log.error("异常:{}", e);
        }
        return true;
    }

/*    public static void main(String[] args) {
        System.out.println(sendSms("18666981551", "8888"));
    }*/

}

